/*
 * @Date: 2025-07-19 01:01:18
 * @LastEditors: 贾二小 erxiao.jia@outlook.com
 * @LastEditTime: 2025-08-09 01:50:03
 * @FilePath: /admin-vue/vite.config.ts
 */
import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import vueDevTools from 'vite-plugin-vue-devtools'
import tailwindcss from '@tailwindcss/vite'
import AutoImport from 'unplugin-auto-import/vite'
import { NaiveUiResolver, VueUseComponentsResolver } from 'unplugin-vue-components/resolvers'
import Components from 'unplugin-vue-components/vite'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import Icons from 'unplugin-icons/vite'
import IconsResolver from 'unplugin-icons/resolver'
import { FileSystemIconLoader } from 'unplugin-icons/loaders'
import path from 'node:path'

// https://vite.dev/config/
export default defineConfig({
  plugins: [
    vue(),
    vueJsx(),
    vueDevTools(),
    tailwindcss(),
    Icons({
      compiler: 'vue3',
      customCollections: {
        ['local']: FileSystemIconLoader(path.join(process.cwd(), 'src/assets/svg-icon'), (svg) =>
          svg.replace(/^<svg\s/, '<svg width="1em" height="1em" '),
        ),
      },
      scale: 1,
      defaultClass: 'inline-block',
    }),
    AutoImport({
      imports: [
        'vue',
        {
          'naive-ui': ['useDialog', 'useMessage', 'useNotification', 'useLoadingBar'],
        },
      ],
      dirs: ['src/composables/**/*', 'src/components/**/*', 'src/enums/**/*', 'src/stores/**/*'],
      dts: 'src/typings/auto-imports.d.ts',
      vueTemplate: true,
      defaultExportByFilename: true,
    }),
    Components({
      resolvers: [
        NaiveUiResolver(),
        VueUseComponentsResolver(),
        IconsResolver({ customCollections: ['local'], componentPrefix: 'icon' }),
      ],
      extensions: ['vue', 'tsx'],
      dirs: ['src/components'],
      dts: 'src/typings/components.d.ts',
      types: [{ from: 'vue-router', names: ['RouterLink', 'RouterView'] }],
    }),
    createSvgIconsPlugin({
      iconDirs: [path.join(process.cwd(), 'src/assets/svg-icon')],
      symbolId: `icon-local-[dir]-[name]`,
      inject: 'body-last',
      customDomId: '__SVG_ICON_LOCAL__',
    }),
  ],
  resolve: {
    alias: {
      '~': fileURLToPath(new URL('./', import.meta.url)),
      '@': fileURLToPath(new URL('./src', import.meta.url)),
    },
  },
  server: {
    host: true,
    proxy: {
      '/api': {
        target: 'http://admin-api.test/',
        changeOrigin: true,
      },
    },
  },
})
